package logic;

import java.util.ArrayList;

import storage.Storage;
import global.CommandType.Type;
import global.Constant;
import global.Time;
import global.Task;

/**@author A0105661M*/
public class MarkCommand extends Command {

	private ArrayList<Integer> indexArray;
	private boolean markAllDone;
	private boolean isExecutable;
	private String feedback;
	private int doneOption;

	public MarkCommand(ArrayList<Integer> indexArray, boolean markAll, boolean isExecutable, String feedback, int doneOrUndone) {
		this.indexArray = indexArray;
		this.markAllDone = markAll;
		this.isExecutable = isExecutable;
		this.feedback = feedback;
		this.doneOption = doneOrUndone;
	}

	public ArrayList<String> execute() {
		ArrayList<String> newFeedback = new ArrayList<String>();

		if (isExecutable) {

			if (markAllDone) {
				indexArray = buildIndexArray(ExecutionHandler
						.getDisplayFileNames().size());
			}

			int counter = 0;
			ArrayList<Task> oldTasks = new ArrayList<Task>();
			ArrayList<Task> updatedTasks = new ArrayList<Task>();

			try {
				for (int i = 0; i < indexArray.size(); i++) {
					if (indexArray.get(i) <= ExecutionHandler
							.getDisplayFileNames().size()) {
						Task oldTask;
						oldTask = Storage.getFileContentAsTask(ExecutionHandler
								.getDisplayFileNames().get(
										indexArray.get(i) - 1));
						Task task = Storage
								.getFileContentAsTask(ExecutionHandler
										.getDisplayFileNames().get(
												indexArray.get(i) - 1));

						if (doneOption == 1){
							task.markDone();
						}else {
							task.markUnDone();
						}
						
						Storage.deleteFileWithoutRecord(oldTask
								.getTaskFileName());
						Storage.addFileWithoutRecord(task);

						oldTasks.add(task);
						updatedTasks.add(oldTask);
						counter++;
					}
				}

				if (counter == 1)
					feedback = (counter + " task has been marked as done.\n");
				else
					feedback = (counter + " tasks have been marked as done.\n");

				ExecutionHandler.setOldTasks(oldTasks);
				ExecutionHandler.setUpdatedTasks(updatedTasks);

				ArrayList<Task> dummyEmptyList = new ArrayList<Task>();
				ExecutionHandler.sortFilename();
				DisplayCommand displayLeft = new DisplayCommand(dummyEmptyList);
				newFeedback = displayLeft.displayTasksLeft();

				if (doneOption == 1){
					ExecutionHandler.setMostRecentCommandType(Type.MARKDONE);
				}else {
					ExecutionHandler.setMostRecentCommandType(Type.MARKUNDONE);
				}
				
				ExecutionHandler.setIsUndo(false);

			} catch (Exception e) {
				newFeedback.add("mark tasks as done nsuccessful.\n");
			}
		}

		newFeedback.add(feedback);
		return newFeedback;
	}
	
	private ArrayList<Integer> buildIndexArray(int size) {
		ArrayList<Integer> result = new ArrayList<Integer>();
		
		for (int i=0; i<size; i++){
			result.add(i+1);
		}
				
		return result;
	}
	
	public String getTestFeedback() {
		String testFeedback = "";
		
		if (markAllDone){
			testFeedback = "all";
		} else {
			for (int i=0; i<indexArray.size(); i++){
				testFeedback += (indexArray.get(i)+" ");
			}
		}
		
		return testFeedback;
	}
	
}
